package com.scx.oper;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.scx.base.MysqlBaseDao;
import com.scx.bean.proc.PorcBean;
import com.scx.bean.proc.ProcDetailBean;
import com.scx.util.DBConfig;


/**
 * 数据库支持类
 * 
 * @author using
 */
public class DBFactoryProc extends MysqlBaseDao {
	// 数据库名称
	private String dbName = DBConfig.getInstance().getProperty("jdbc.database");

	/**
	 * 查询全部的存储过程
	 * 
	 * @author using using@qq.com
	 */
	public List<PorcBean> queryAllProc() {
		List<PorcBean> list = new ArrayList<PorcBean>();
		try {
			String sql = "SELECT name, type FROM mysql.proc WHERE db = '" + dbName + "' AND `type` = 'PROCEDURE';";
			super.open();
			ResultSet resultSet = super.findSql(sql);
			while (resultSet.next()) {
				PorcBean porcBean = new PorcBean();
				porcBean.setName(resultSet.getString("name"));
				porcBean.setType(resultSet.getString("type"));
				list.add(porcBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			super.close();
		}
		return list;
	}

	/**
	 * 查询单个存储过程详细信息
	 * 
	 * @author using using@qq.com
	 */
	public List<ProcDetailBean> queryProcDetails() {
		List<ProcDetailBean> list = new ArrayList<ProcDetailBean>();
		try {
			String sql = "SELECT specific_name,parameter_mode, parameter_name, data_type FROM information_schema.PARAMETERS WHERE SPECIFIC_SCHEMA = '" + dbName + "' ORDER BY ORDINAL_POSITION;";
			super.open();
			ResultSet resultSet = super.findSql(sql);
			while (resultSet.next()) {
				ProcDetailBean porcBean = new ProcDetailBean();
				porcBean.setMode(resultSet.getString("parameter_mode"));
				porcBean.setName(resultSet.getString("parameter_name"));
				porcBean.setType(resultSet.getString("data_type"));
				porcBean.setProcName(resultSet.getString("specific_name"));
				list.add(porcBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			super.close();
		}
		return list;
	}

}
